import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;


public class TestTables {
	public static void main(String[] args) {
		/*
		try {
			System.setErr(new PrintStream(new File("/dev/null")));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		*/
		
		System.out.println("Testing admin");
		HBaseAdmin admin = null;
		HBaseConfiguration config = new HBaseConfiguration();
		try {			
			admin = new HBaseAdmin(config);
			// Delete all tables
			System.out.println("The following tables are found and are being deleted:");
			for (HTableDescriptor table : admin.listTables()) {
				String name = table.getNameAsString();
				System.out.println(name);
				admin.disableTable(name);
				admin.deleteTable(name);
			}
			System.out.println("Done");
			HTableDescriptor table = new HTableDescriptor("TestTable");
			table.addFamily(new HColumnDescriptor("TestFamily"));
			admin.createTable(table);
			admin.shutdown();
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("Testing client");
		
	    HTable table;
		try {
			System.out.println("Inserting test data");
			table = new HTable(config, "TestTable");
			Put p = new Put(Bytes.toBytes("Row1"));
			p.add(Bytes.toBytes("TestFamily"), Bytes.toBytes("col1"), Bytes.toBytes("value11"));
			table.put(p);
			
			System.out.println("Fetching inserted data");
			
			Scan s = new Scan();
		    ResultScanner scanner = table.getScanner(s);
			for (Result rr : scanner) {
		        System.out.println("Found row: " + rr);
		      }
			scanner.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

